package com.iias.communication.business;

import com.alibaba.fastjson.JSON;
import com.iias.common.constants.content.ProcessContext;
import com.iias.common.util.log.LogUtils;
import com.iias.communication.domain.InstructionInfo;
import com.iias.communication.enums.Protocol;
import com.iias.communication.packet.PacketBody;
import com.iias.communication.packet.ProPacket;
import io.netty.channel.Channel;
import org.slf4j.Logger;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 *  指令执行层
 */
public class BusinessActuator {

    private final static Logger log = LogUtils.getLogger();

    public static void  actuator(Channel channel, ProPacket proPacket){
        ProcessContext.add(channel);
        int msgId = proPacket.getHeard().getMsgId();
        InstructionInfo instructionInfo = BusinessServiceMap.getRequest(msgId);
        if (instructionInfo == null)log.info(" msgId : "+ msgId +";无可用的业务处理器");
        Method method =instructionInfo.getMethod();
        if (method == null)log.info("msgId :" + msgId + ": 无可用的指令处理方法 ");
        try {
            log.info(" msgId: "+ msgId + "指令数据接收;" + JSON.toJSONString(proPacket.getPacketBody()));
            Object invoke = method.invoke(instructionInfo.getBusinessSerializable(), proPacket.getPacketBody());
            if (invoke == null){
                log.info( "msgId: " + msgId + "无数据回复;");
                return;
            }

            InstructionInfo response = BusinessServiceMap.getResponse((PacketBody) invoke);
            Protocol protocol = Protocol.valueOf(response.getDecodeFlag());
            if (protocol == null){
                log.info( "msgId: " + response.getMsgId() + "找不到对应的编码信息;");
            }
            byte[] encoding = protocol.getDecode().encoding(response.getMsgId(), (PacketBody) invoke);
            send(channel,encoding,response.getMsgId());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }finally {
            ProcessContext.clean();
        }
    }


    private static void send(Channel channel,byte[] encoding,int msgId){
        boolean success = channel.writeAndFlush(encoding).isSuccess();
        if (success)log.info("request msgId : "+ msgId + " ; response msgId :" +msgId + "发送消息成功" );
        if (!success)log.info("request msgId : "+ msgId + " ; response msgId :" +msgId + "发送消息失败" );
    }


    public static void sendMsg(Channel channel, PacketBody packetBody){
        InstructionInfo response = BusinessServiceMap.getResponse(packetBody);
        if (response == null){
            log.info("sendMsg : 找不到对应的编码信息;");
            return;
        }
        Integer msgId  = response.getMsgId();
        Protocol protocol = Protocol.valueOf(response.getDecodeFlag());
        byte[] encoding = protocol.getDecode().encoding(response.getMsgId(), packetBody);
        send(channel,encoding,msgId);
    }

    public static void sendMsg( PacketBody packetBody){
        Channel channel = ProcessContext.getObj(Channel.class);
        InstructionInfo response = BusinessServiceMap.getResponse(packetBody);
        if (response == null){
            log.info("sendMsg : 找不到对应的编码信息;");
            return;
        }
        Integer msgId  = response.getMsgId();
        Protocol protocol = Protocol.valueOf(response.getDecodeFlag());
        byte[] encoding = protocol.getDecode().encoding(response.getMsgId(), packetBody);
        send(channel,encoding,msgId);
    }
}
